package com.cssy.xxkj.logic.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtheroutDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtheroutItemDAO;
import com.cssy.xxkj.logic.entity.LogicExcelToStoreDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutItemDO;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherOutService;
import com.cssy.xxkj.logic.service.ILogicWarehousePriceService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicWareHouseOtherOutService")
public class LogicWarehouseOtheroutServiceImpl implements ILogicWareHouseOtherOutService{

	@Resource(name = "logicWarehouseOtheroutItemDAO")
	private LogicWarehouseOtheroutItemDAO logicWarehouseOtheroutItemDAO;
	
	@Resource(name = "logicWarehouseOtheroutDAO")
	private LogicWarehouseOtheroutDAO logicWarehouseOtheroutDAO;
	
	@Resource(name = "logicWarehousePriceService")
	private ILogicWarehousePriceService logicWarehousePriceService;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Resource(name = "oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWarehouseOtheroutDO>  list= logicWarehouseOtheroutDAO.find(scs);
		rs.setRecordCount(logicWarehouseOtheroutDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public List<LogicWarehouseOtheroutItemDO> findItemListByMap(Map<String, Object> paramMap)
			throws SyBaseException{
		return logicWarehouseOtheroutItemDAO.findListByMap(paramMap);
	}
	
	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicWarehouseOtheroutDAO.deleteLogicWarehouseOtheroutDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicWarehouseOtheroutDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return logicWarehouseOtheroutDAO.updateLogicWarehouseOtheroutDO(t);
	}

	@Override
	public LogicWarehouseOtheroutDO save(LogicWarehouseOtheroutDO t, User user)
			throws SyBaseException {
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		t.setBizVersion(1);
		logicWarehouseOtheroutDAO.insertLogicWarehouseOtheroutDO(t);
		return t;
	}

	@Override
	public LogicWarehouseOtheroutDO findListById(String id) throws SyBaseException {
		return logicWarehouseOtheroutDAO.findLogicWarehouseOtheroutDOByPrimaryKey(id);
	}
	
	@Override
	public void outSave(LogicWarehouseOtheroutDO curAP,HttpServletRequest request) throws SyBaseException {
		try{
			String json = request.getParameter("addressInfos");
			List<LogicWarehouseOtheroutItemDO> axxlist = JSONArray.parseArray(json,LogicWarehouseOtheroutItemDO.class);
			//查询物料出库时的平均价格
			SearchConditions foldScs = new SearchConditions();
			Map<String, BigDecimal> zdMaterFoldMap = logicWarehousePriceService.findQueryMaterOutFoldToMap(foldScs);
			Map<String, LogicExcelToStoreDO> excelPrice=this.oftenMapUtil.getlogicExcelToStoreDOMap(foldScs);//初始表数据
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				if(!curAP.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)){
					String zd010OtherDate = request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					logicWarehouseOtheroutItemDAO.deleteByPid(curAP.getId());
					for (LogicWarehouseOtheroutItemDO lii : axxlist){
						String id = IDCodeTypeUtil.getId();
						lii.setId(id);
						lii.setZd001OtherOutId(curAP.getId());
						logicWarehouseOtheroutItemDAO.insertLogicWarehouseOtheroutItemDO(lii);
					}
				}
				if (curAP.getZd003BillStatus().equals(BillStatusUtil.OTHERWH_BILL_STATUE_FOUR)){
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					LogicWarehouseOtheroutDO loi = logicWarehouseOtheroutDAO.findLogicWarehouseOtheroutDOByPrimaryKey(curAP.getId());
					for (LogicWarehouseOtheroutItemDO lii : axxlist) {
						String keyStr = (StringUtils.isEmpty(lii.getZd002MaterialsId())?null:lii.getZd002MaterialsId())+"_"+
								(StringUtils.isEmpty(lii.getZd002MaterialsCode())?null:lii.getZd002MaterialsCode())+"_"+
								(StringUtils.isEmpty(lii.getZd002MaterialsName())?null:lii.getZd002MaterialsName())+"_"+
								(StringUtils.isEmpty(lii.getZd004MaterialsModel())?null:lii.getZd004MaterialsModel())+"_"+
								(StringUtils.isEmpty(lii.getZd003MaterialsSpec())?null:lii.getZd003MaterialsSpec())+"_"+
								(StringUtils.isEmpty(lii.getZd016MaterialsColor())?null:lii.getZd016MaterialsColor())+"_"+
								(StringUtils.isEmpty(lii.getZd017MaterialsQuanlity())?null:lii.getZd017MaterialsQuanlity());
						if(lii.getZd009Price()==null){
							if(zdMaterFoldMap==null||zdMaterFoldMap.get(keyStr)==null){
								LogicExcelToStoreDO ets=excelPrice.get(keyStr);
								if(ets==null){
									lii.setZd009Price(BigDecimal.ZERO);
								}else{
									lii.setZd009Price(ets.getZd010MaterialEndMoney().divide(ets.getZd010MaterialEndNumber(),4));
								}
							}else{
								lii.setZd009Price(zdMaterFoldMap.get(keyStr));
							}
							this.logicWarehouseOtheroutItemDAO.updateLogicWarehouseOtheroutItemDO(lii);
						}
						logicHsUtilService.processHsOtherOutItem(loi, lii, SessionUtil.getUser(request));
					}
				}
			}else{
					curAP.setZd001OutCode(syCodeService.getCode(BillTypeConstant.OTHEROUTWH,BillTypeConstant.OTHEROUTWH,SyCode.DATE_YYMM,5));
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					String zd010OtherDate = request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					this.save(curAP, SessionUtil.getUser(request));
					for (LogicWarehouseOtheroutItemDO lii : axxlist){
						lii.setId(IDCodeTypeUtil.getId());
						lii.setZd001OtherOutId(id);
						String keyStr = (StringUtils.isEmpty(lii.getZd002MaterialsId())?null:lii.getZd002MaterialsId())+"_"+
								(StringUtils.isEmpty(lii.getZd002MaterialsCode())?null:lii.getZd002MaterialsCode())+"_"+
								(StringUtils.isEmpty(lii.getZd002MaterialsName())?null:lii.getZd002MaterialsName())+"_"+
								(StringUtils.isEmpty(lii.getZd004MaterialsModel())?null:lii.getZd004MaterialsModel())+"_"+
								(StringUtils.isEmpty(lii.getZd003MaterialsSpec())?null:lii.getZd003MaterialsSpec())+"_"+
								(StringUtils.isEmpty(lii.getZd016MaterialsColor())?null:lii.getZd016MaterialsColor())+"_"+
								(StringUtils.isEmpty(lii.getZd017MaterialsQuanlity())?null:lii.getZd017MaterialsQuanlity());
						if(lii.getZd009Price()==null){
							if(zdMaterFoldMap==null||zdMaterFoldMap.get(keyStr)==null){
								LogicExcelToStoreDO ets=excelPrice.get(keyStr);
								if(ets==null){
									lii.setZd009Price(BigDecimal.ZERO);
								}else{
									lii.setZd009Price(ets.getZd010MaterialEndMoney().divide(ets.getZd010MaterialEndNumber(),4));
								}
							}else{
								lii.setZd009Price(zdMaterFoldMap.get(keyStr));
							}
							this.logicWarehouseOtheroutItemDAO.updateLogicWarehouseOtheroutItemDO(lii);
						}
						logicWarehouseOtheroutItemDAO.insertLogicWarehouseOtheroutItemDO(lii);
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicWarehouseOtheroutDAO.deleteBizRemoved(id);
	}
}
